package com.sensePlusVR.fragment;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sensePlusVR.base.BaseFragment;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.MyLog;

@ContentView(R.layout.fragment_followa)
public class FollowAFragment extends BaseFragment implements OnClickListener {
	private static final String TAG = FollowAFragment.class.getSimpleName();
	private View rootView;
	@ViewInject(R.id.follow_frame)
	private FrameLayout follow_frame;
	
	@ViewInject(R.id.follow_follow_tv)
	private TextView follow_follow_tv;
	
	@ViewInject(R.id.follow_all_tv)
	private TextView follow_all_tv;
	
	private FragmentManager fragmentManager;
	
	private FollowFollowFragment followFollowFragment;
	private FollowAllFragment followAllFragment;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = x.view().inject(this, inflater, container);
		init();
		return rootView;
	}

	private void init() {
		fragmentManager = getActivity().getSupportFragmentManager();

		follow_follow_tv.setOnClickListener(this);
		follow_all_tv.setOnClickListener(this);

		setTabSelection(0);

	}
	
	public void setTabSelection(int index) {
		clearSelection(index);
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		hideFragments(transaction);
		switch (index) {
		case 0:
			if (followFollowFragment == null) {
				MyLog.d(TAG, "followFollowFragment == nulll");
				followFollowFragment = new FollowFollowFragment();
				transaction.add(R.id.follow_frame, followFollowFragment);
			} else {
				MyLog.d(TAG, "followFollowFragment != nulll");
				transaction.show(followFollowFragment);
			}
			break;

		case 1:
			if (followAllFragment == null) {
				MyLog.d(TAG, "followAllFragment == nulll");
				followAllFragment = new FollowAllFragment();
				transaction.add(R.id.follow_frame, followAllFragment);

			} else {
				MyLog.d(TAG, "followAllFragment != nulll");
				transaction.show(followAllFragment);
			}
			break;
		}
		transaction.commit();
	}
	
	private void clearSelection(int index) {
		follow_follow_tv.setTextColor(getResources().getColor(R.color.app_text_color_blue1));
		follow_all_tv.setTextColor(getResources().getColor(R.color.app_text_color_blue1));
		if (index == 0) {
			follow_follow_tv.setTextColor(getResources().getColor(R.color.app_text_color_gray1));
		}
		if (index == 1) {
			follow_all_tv.setTextColor(getResources().getColor(R.color.app_text_color_gray1));
		}
	}
	
	private void hideFragments(FragmentTransaction transaction) {
		if (followFollowFragment != null) {
			transaction.hide(followFollowFragment);
		}
		if (followAllFragment != null) {
			transaction.hide(followAllFragment);
		}
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.follow_follow_tv:
			setTabSelection(0);
			break;

		case R.id.follow_all_tv:
			setTabSelection(1);
			break;
		}
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		followFollowFragment = null;
		followAllFragment = null;
	}

}
